﻿using MCPlatform.Models.Motions;
using MCPlatform.UI.Controls.Models;
using MCPlatform.ViewModels.Motion;
using Panuon.WPF;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.Models
{
    public class WorkStationModel : NotifyPropertyChangedBase
    {
        #region Properties

        public string WorkStationName { get => _WorkStationName; set => Set(ref _WorkStationName, value); }
        private string _WorkStationName = string.Empty;

        public ushort WorkStationID { get => _WorkStationID; set => Set(ref _WorkStationID, value); }
        private ushort _WorkStationID;

        public string WorkStationKey { get => _WorkStationKey; set => Set(ref _WorkStationKey, value); }
        private string _WorkStationKey = string.Empty;

        public ushort AxisCount 
        { 
            get => _AxisCount;
            set 
            { 
                Set(ref _AxisCount, value);

                if (value < 1)
                {
                    return;
                }
                if (AxisModels != null)
                {
                    AxisModels.Clear();
                    for (int i = 0; i < AxisCount; i++)
                    {
                        AxisModels.Add(new AxisModel());
                    }
                }

                

            }
        }
        private ushort _AxisCount;


        public ushort CoordinateCount
        {
            get => _CoordinateCount;
            set
            {
                Set(ref _CoordinateCount, value);

                if (value < 1)
                {
                    return;
                }
                if (CoordinateModels != null)
                {
                    CoordinateModels.Clear();
                    for (int i = 0; i < CoordinateCount; i++)
                    {
                        CoordinateModels.Add(new CoordinateModel());
                    }
                }



            }
        }
        private ushort _CoordinateCount;

        public ushort ValveCount
        {
            get => _ValveCount;
            set
            {
                Set(ref _ValveCount, value);

                if (value < 1)
                {
                    return;
                }

                if (CylinderModels != null)
                {
                    CylinderModels.Clear();
                    for (int i = 0; i < ValveCount; i++)
                    {
                        CylinderModels.Add(new CylinderModel());
                    }
                }
            }
        }
                
        private ushort _ValveCount;


        public ushort IOCount
        {
            get => _IOCount;
            set
            {
                Set(ref _IOCount, value);

                if (value < 1)
                {
                    return;
                }

                if (IOModels != null)
                {
                    IOModels.Clear();
                    for (int i = 0; i < IOCount; i++)
                    {
                        IOModels.Add(new IOModel());
                    }
                }
            }
        }

        private ushort _IOCount;


        public ushort PointsCount
        {
            get => _PointsCount;
            set
            {
                Set(ref _PointsCount, value);

                if (value < 1)
                {
                    return;
                }

                if (PointSettingModels != null)
                {
                    PointSettingModels.Clear();
                    for (int i = 0; i < PointsCount; i++)
                    {
                        PointSettingModels.Add(new PointSettingModel() { });
                    }
                }
            }
        }

        private ushort _PointsCount;


        public ushort RobotCount
        {
            get => _RobotCount;
            set
            {
                Set(ref _RobotCount, value);

                if (value < 1)
                {
                    return;
                }

                if (RobotModels != null)
                {
                    RobotModels.Clear();
                    for (int i = 0; i < _RobotCount; i++)
                    {
                        RobotModels.Add(new RobotModel() { });
                    }
                }
            }
        }

        private ushort _RobotCount;

        public ObservableCollection<AxisModel> AxisModels { get => _AxisModels; set => Set(ref _AxisModels, value); } 
        private ObservableCollection<AxisModel> _AxisModels = new ObservableCollection<AxisModel>();


        public ObservableCollection<CoordinateModel> CoordinateModels { get => _CoordinateModels; set => Set(ref _CoordinateModels, value); }
        private ObservableCollection<CoordinateModel> _CoordinateModels = new ObservableCollection<CoordinateModel>();

        public ObservableCollection<CylinderModel> CylinderModels { get => _CylinderModels; set => Set(ref _CylinderModels, value); }
        private ObservableCollection<CylinderModel> _CylinderModels = new ObservableCollection<CylinderModel>();

        public ObservableCollection<IOModel> IOModels { get => _IOModels; set => Set(ref _IOModels, value); }
        private ObservableCollection<IOModel> _IOModels = new ObservableCollection<IOModel>();


        public ObservableCollection<PointSettingModel> PointSettingModels { get => _PointSettingModels; set => Set(ref _PointSettingModels, value); }
        private ObservableCollection<PointSettingModel> _PointSettingModels = new ObservableCollection<PointSettingModel>();


        public ObservableCollection<RobotModel> RobotModels { get => _RobotModels; set => Set(ref _RobotModels, value); }
        private ObservableCollection<RobotModel> _RobotModels = new ObservableCollection<RobotModel>();

        #endregion

        #region Methods

        public void ActivedMotionMapping()
        {
            foreach (var model in AxisModels)
            {
                model.StartMappingEntityToModel();
                model.MotionParaModel.MappingFromEntity();
            }
        }


        public void DeactivedMotionMapping()
        {
            foreach (var model in AxisModels)
            {
                model.StopMappingEntityToModel();
            }
        }


        public void ActivedCoordianteMapping()
        {
            foreach (var model in CoordinateModels)
            {
                model.StartMappingEntityToModel();
                model.ImpMotionParaModel.MappingFromEntity();
            }
        }


        public void DeactivedCoordinateMapping()
        {
            foreach (var model in CoordinateModels)
            {
                model.StopMappingEntityToModel();
            }
        }


        public void ActivedValveMapping()
        {
            foreach (var model in CylinderModels)
            {
                model.StartMappingEntityToModel();
                model.ValveParaModel.MappingFromEntity();
            }
        }


        public void DeactivedValveMapping()
        {
            foreach (var model in CylinderModels)
            {
                model.StopMappingEntityToModel();
            }
        }


        public void ActivedIOMapping()
        {
            foreach (var model in IOModels)
            {
                model.StartMappingEntityToModel();
                model.IOParaModel.MappingFromEntity();
            }
        }


        public void DeactivedIOMapping()
        {
            foreach (var model in IOModels)
            {
                model.StopMappingEntityToModel();
            }
        }
        #endregion
    }
}
